<?php

namespace App\Repositories;

use App\Exceptions\BasicException;

class RoomPkRepository
{
    /**
     * Notes:获取pk列表
     * User: wangyueli
     *
     * @param $params
     * @return mixed
     */
    public function list($params)
    {
        $page = $params['page'] ?? 1;
        $perPage = $params['perPage'] ?? 15;
        $room_id = $params['room_id'] ?? 0;
        $room_title = $params['room_title'] ?? '';
        $owner_user_id = $params['owner_user_id'] ?? 0;
        $owner_nickname = $params['owner_nickname'] ?? '';

        //条件拼接
        $where = [];
        if ($room_title) {
            $where[] = ['r.room_name', 'like', "%$room_title%"];
        }

        //room_id
        if ($room_id) {
            $where[] = ['r.room_id', '=', $room_id];
        }

        //房主编号
        if ($owner_user_id) {
            $where[] = ['u.user_code', '=', $owner_user_id];
        }

        //房主昵称
        if ($owner_nickname) {
            $where[] = ['u.nickname', 'like', "%$owner_nickname%"];
        }

        //查询已结束的pk记录
        $list = app('model_room_pk')
            ->from('ln_room_pk as p')
            ->selectRaw('p.id,p.status,p.room_id,p.pk_time,p.total_charm,p.win_flag,u.nickname,r.room_name,r.user_id,u.user_code,r.room_id as room_code')
            ->leftJoin('ln_user_room as r', 'r.id', '=', 'p.room_id')
            ->leftJoin('ln_user_data as u', 'u.user_id', '=', 'r.user_id')
            ->where('status',2)
            ->where($where)
            ->orderBy('p.id', 'desc')
            ->paginate($perPage, ['*'], '', $page);

        $list->getCollection()->transform(function ($value) {

            //把用户编号和房间编号和房间名称赋值给相应字段
            $value->room_title = $value->room_name;
            $value->owner_user_id = $value->user_code;
            $value->room_id = $value->room_code;

            unset($value->user_id);
            unset($value->user_code);
            unset($value->room_code);
            return $value;
        });


        return $list;
    }

    /**
     * Notes:获取pk详情
     * User: wangyueli
     *
     * @param $id
     * @return mixed
     * @throws BasicException
     */
    public function info($id)
    {
        $info = app('model_room_pk')
            ->from('ln_room_pk as p')
            ->selectRaw('p.id,p.status,p.room_id,p.pk_time,p.total_charm,p.win_flag,p.pit_info,u.nickname,r.room_name,r.user_id,u.user_code,r.room_id as room_code')
            ->leftJoin('ln_user_room as r', 'r.id', '=', 'p.room_id')
            ->leftJoin('ln_user_data as u', 'u.user_id', '=', 'r.user_id')
            ->where('p.id', $id)
            ->first();
        if (empty($info)) {
            throw  new BasicException(96100);
        }

        //麦位数据拼接
        $pit_info = json_decode($info->pit_info, true)['e_p_charm'] ?? [];
        //左边麦位 1,2,5,6
        $left = [];
        $left_total = 0;
        //右边麦位 3,4,7,8
        $right = [];
        $right_total = 0;
        foreach ($pit_info as $k => $v) {
            if (in_array($k, [1, 2, 5, 6])) {
                $left[] = ['pit' => $k, 'amount' => $v];
                $left_total += $v;
            } else {
                $right[] = ['pit' => $k, 'amount' => $v];
                $right_total += $v;
            }
        }

        $info['left'] = ['data' => $left, 'total' => $left_total];
        $info['right'] = ['data' => $right, 'total' => $right_total];;

        //获取房间加入的公会信息
        $sociaty = app('model_sc_hall')->where('room_id', $info['room_id'])->where('status', 3)->first();
        $info['sociaty_id'] = $sociaty->sociaty->sociaty_no ?? '';
        $info['sociaty_name'] = $sociaty->sociaty->sociaty_name ?? '';

        unset($info->pit_info);

        //把用户编号和房间编号和房间名称赋值给相应字段
        $info->room_title = $info->room_name;
        $info->owner_user_id = $info->user_code;
        $info->room_id = $info->room_code;

        unset($info->user_id);
        unset($info->room_name);
        unset($info->user_code);
        unset($info->room_code);

        return $info;
    }
}
